/**
 * @author: 子龙
 */
package com.fzl.lotterysystem.controller;

import com.fzl.lotterysystem.common.errorcode.ControllerErrorCodeConstants;
import com.fzl.lotterysystem.common.exception.ControllerException;
import com.fzl.lotterysystem.common.pojo.CommonResult;
import com.fzl.lotterysystem.common.utils.JacksonUtil;
import com.fzl.lotterysystem.controller.param.CreatePrizeParam;
import com.fzl.lotterysystem.controller.param.PageParam;
import com.fzl.lotterysystem.controller.result.FindPrizeListResult;
import com.fzl.lotterysystem.service.DTO.PageListDTO;
import com.fzl.lotterysystem.service.DTO.PrizeDTO;
import com.fzl.lotterysystem.service.PictureService;
import com.fzl.lotterysystem.service.PrizeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/prize")
@Slf4j
public class PrizeController {
    @Autowired
    private PictureService pictureService;
    @Autowired
    private PrizeService prizeService;
    @RequestMapping("/uploadPic")
    public String uploadPic(MultipartFile multipartFile){
        return pictureService.savePicture(multipartFile);
    }

    /**
     * 创建奖品，表单提交
     * @param param
     * @param picFile
     * @return
     */
    @RequestMapping("/createPrize")
    public CommonResult<Long> createtPrize(@Validated @RequestPart("param") CreatePrizeParam param,
                                           @RequestPart("prizePic") MultipartFile picFile){
        log.info("createtPrize CreatePrizeParam{}", JacksonUtil.writeValueAsString(param));
        return CommonResult.success(prizeService.createPrize(param, picFile));
    }

    @RequestMapping("/findList")
    public CommonResult<FindPrizeListResult> findPrizeList(PageParam param ){
        log.info("findPrizeList FindPrizeListParam{}", JacksonUtil.writeValueAsString(param));
       PageListDTO<PrizeDTO> pageListDTO= prizeService.findPrizeList(param);
       return CommonResult.success(converToFindPrizeListResult(pageListDTO));
    }

    private FindPrizeListResult converToFindPrizeListResult(PageListDTO<PrizeDTO> pageListDTO) {
        if(pageListDTO==null){
             throw new ControllerException(ControllerErrorCodeConstants.FIND_PRIZE_LIST_ERROR);
        }
        FindPrizeListResult findPrizeListResult = new FindPrizeListResult();
        findPrizeListResult.setTotal(pageListDTO.getTotal());
        findPrizeListResult.setRecords(pageListDTO.getRecords()
                .stream()
                .map(prizeDTO -> {
                    FindPrizeListResult.PrizeInfo prizeInfo = new FindPrizeListResult.PrizeInfo();
                    prizeInfo.setPrizeId(prizeDTO.getPrizeId());
                    prizeInfo.setPrizeName(prizeDTO.getName());
                    prizeInfo.setDescription(prizeDTO.getDescription());
                    prizeInfo.setImageUrl(prizeDTO.getImageUrl());
                    prizeInfo.setPrice(prizeDTO.getPrice());
                    return prizeInfo;
                }).collect(Collectors.toList())
        );
        return findPrizeListResult;
    }
}